package com.tct.dubbo;


import com.tct.dto.StorageVo;
import com.tct.entity.AccountA;
import com.tct.service.AccountService;
import com.yiqiniu.easytrans.core.EasyTransFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.AccountADto;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author TcT
 * Date: 2019/6/28.
 * Time: 上午12:21.
 * @Title:
 * @Description:
 */
@Service
public class ADubboServiceImpl implements ADubboService {

    public static final String BUSINESS_CODE = "buySth";


    @Resource
    AccountService accountService;

    @Resource
    BDubboService bDubboService;

    /**
     * 事务
     */
    @Resource
    private EasyTransFacade transaction;





    @Transactional
    @Override
    public String save(AccountADto accountA,Long transactionId) {

        //先完成本地事务
        //优先完成本地事务以 1. 提高性能（减少异常时回滚消耗）2. 生成事务内交易ID
        AccountA accountA1 = new AccountA();
        BeanUtils.copyProperties(accountA,accountA1);
        accountService.save(accountA1);
        System.out.println("a服务保存成功,假设执行成功后返回id=8");

        /**
         * 声明全局事务ID，其由appId,业务代码，业务代码内ID构成
         * 本代码可以省略，框架会自动生成ID及使用一个默认的业务代码
         * 但这样的话，会使得我们难以把全局事务ID与一个具体的事务关联起来
         */
        transaction.startEasyTrans(BUSINESS_CODE, transactionId);


        //调用b-dubbo服务
        System.out.println("调用b服务,并把Count值改为id:8");
        StorageVo storage = new StorageVo();
        storage.setCount(8);
        storage.setCommodityCode("dubbo");
        bDubboService.save(storage);

//        @SuppressWarnings("unused")
//        Future<FutureResult> deductFuture = transaction.execute(bDubboService);


        return "全部操作成功";

    }

    @Override
    public List<AccountADto> findAll() {
        List<AccountA> all = accountService.findAll();
        return null;
    }
}
